<?php 
if($surveyCore->Survey)
{
	$form = 'guide.php';
	
	if($surveyCore->Survey->Password)
	{ $form = 'password.php';  }
	
	if($_GET['print'] > 0)
	{ $form = 'print.php'; }
	
	switch ($_REQUEST['action'])
	{
		case 'password':
			if($surveyCore->Survey->Password == $_POST['password'])
			{ 
				if($surveyCore->Survey->Description)
				{ $form = 'guide.php'; }
				else
				{
					if($surveyCore->Survey->Demogs)
					{ $form = 'demography.php'; }
					else
					{ $form = 'sections.php'; }
				}
			}
		break;
		case 'guide':
			if(count($surveyCore->Forms['guide']->Fields) > 0)
			{
				$surveyCore->Forms['guide']->validate($txt, $com);
				if($surveyCore->Forms['guide']->Valid)
				{ 
					if($surveyCore->Survey->Demogs)
					{ $form = 'demography.php'; }
					else
					{ $form = 'sections.php'; }
				}
				else
				{ $form = 'guide.php'; }
			}
			else
			{ 
				if($surveyCore->Survey->Demogs)
				{ $form = 'demography.php'; }
				else
				{ $form = 'sections.php'; }
			}
		break;
		case 'demography':
			$surveyCore->Forms['demog']->validate($txt, $com);
			if($surveyCore->Forms['demog']->Valid)
			{ $form = 'sections.php'; }
			else
			{ $form = 'demography.php'; }
		break;
		case 'sections':
			$form = 'sections.php';
			$surveyCore->Forms['sections']->validate($txt, $com);
			if($surveyCore->Forms['sections']->Valid)
			{ 
				if($surveyCore->Survey->Sections)
				{
					$surveyCore->Response = new SurveyResponse();
					$surveyCore->Response->setSurveyId($surveyCore->Survey->Id);
					$surveyCore->Response->setSubject($_POST['subject']);
					
					if($surveyCore->Survey->Demogs)
					{
						foreach($surveyCore->Survey->Demogs as $demog)
						{ 
							if($demog->Values)
							{
								if(isset($surveyCore->Forms['sections']->Fields['demog_'.$demog->Id]))
								{
									$surveyCore->Response->DemographyValues[$demog->Id] = new ResponseDemographyValue();
									$surveyCore->Response->DemographyValues[$demog->Id]->setSurveyId($surveyCore->Survey->Id);
									$surveyCore->Response->DemographyValues[$demog->Id]->setValue($surveyCore->Forms['sections']->Fields['demog_'.$demog->Id]->getValue());
								}
							}
						}
					}
					$questions=0;
					$i=0;
					foreach($surveyCore->Survey->Sections as $section)
					{
						if($section->Questions)
						{
							$questions += count($section->Questions);
							foreach($section->Questions as $question)
							{
								switch($section->Type)
								{
									case 0:
										$surveyCore->Response->AnswerValuesScale[$i] = new AnswerValueScale();
										$surveyCore->Response->AnswerValuesScale[$i]->setQuestionId($question->Id);
										if($question->isValidAnswer($_POST['q_'.$question->Id.'_1']) && $question->isValidAnswer($_POST['q_'.$question->Id.'_2']))
										{ $surveyCore->Response->AnswerValuesScale[$i]->setValid(true); }
										if($surveyCore->Response->AnswerValuesScale[$i]->getValid())
										{
											$surveyCore->Response->AnswerValuesScale[$i]->setValue($_POST['q_'.$question->Id.'_1']);
											$surveyCore->Response->AnswerValuesScale[$i]->setValue2($_POST['q_'.$question->Id.'_2']);
										}
										$i++;
									break;
									case 1:
										$surveyCore->Response->AnswerValuesScale[$i] = new AnswerValueScale();
										$surveyCore->Response->AnswerValuesScale[$i]->setQuestionId($question->Id);
										$surveyCore->Response->AnswerValuesScale[$i]->setValid($question->isValidAnswer($_POST['q_'.$question->Id.'_1']));
										if($surveyCore->Response->AnswerValuesScale[$i]->getValid())
										{ $surveyCore->Response->AnswerValuesScale[$i]->setValue($_POST['q_'.$question->Id.'_1']); }
										$i++;
									break;
									case 2:
										$surveyCore->Response->AnswerValuesScale[$i] = new AnswerValueScale();
										$surveyCore->Response->AnswerValuesScale[$i]->setQuestionId($question->Id);
										$surveyCore->Response->AnswerValuesScale[$i]->setValid($question->isValidAnswer($_POST['q_'.$question->Id.'_2']));
										if($surveyCore->Response->AnswerValuesScale[$i]->getValid())
										{ $surveyCore->Response->AnswerValuesScale[$i]->setValue($_POST['q_'.$question->Id.'_2']); }
										$i++;
									break;
								}
							}
						}
					}
					$surveyCore->Response->setQuesCount($questions);
					if($surveyCore->Response->checkSetValid())
					{ 
						$surveyCore->Response->insertResponse();
						$form = 'complete.php'; 
					}
				}
			}
			
		break;
	}
}

?>